/**
 * <p>Copyright (c) 2010 by DRM. </p>
 */

package com.android.drm.util;

import java.util.List;

import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.graphics.Color;
import android.os.Bundle;

import com.android.drm.DRMApplication;
import com.android.drm.to.NineOfFourReportTO;

/**
 * <p>
 * <b>CLASS:</b> com.android.drm.util.ChartHelp
 * </p>
 * 
 * ...........................................................................
 * <p>
 * <b>DESCRIPTION:</b><br/>
 * The helper class of charting.
 * </p>
 * 
 * ...........................................................................
 * <p>
 * <b>MODIFICATION HISTORY:</b>
 * <p>
 * [2010/07/29] e2005g: Initial version.
 * [2010/08/25] michellem: Added methods for bar chart for correct hits.
 * </p>
 * </p>
 * 
 * ...........................................................................
 * 
 * @author e2005g
 * @since 1.0
 */

public class ChartHelper {

	public static XYMultipleSeriesRenderer getXYChartRenderer() {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		XYSeriesRenderer r = new XYSeriesRenderer();
		r.setColor(Color.BLUE);
		r.setPointStyle(PointStyle.SQUARE);
		r.setFillBelowLine(true);
		r.setFillBelowLineColor(Color.WHITE);
		r.setFillPoints(true);
		renderer.addSeriesRenderer(r);
		r = new XYSeriesRenderer();
		r.setPointStyle(PointStyle.CIRCLE);
		r.setColor(Color.MAGENTA);
		r.setFillPoints(true);
		renderer.addSeriesRenderer(r);
		renderer.setAxesColor(Color.BLACK); 
		renderer.setLabelsColor(Color.BLACK);
		renderer.setChartTitle("Four Of Nine");
		return renderer;
	}

	public static XYMultipleSeriesDataset getFourOfNineXYDataset(
			DRMApplication drmApplication, Bundle bundle) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		NineOfFourReportTO reportTo = drmApplication.getDataHelper()
				.getAverageIntervals(bundle.getLong("userId"),
						bundle.getLong("actionId"),
						bundle.getString("userName"),
						bundle.getString("actionName"));
		List<Double> averageList = reportTo.getAverages();
		List<Long> totalList = reportTo.getTotals();

		XYSeries series = new XYSeries(bundle.getString("averageTitle"));
		for (int i = 0; i < reportTo.getLength(); i++) {
			series.add(i, averageList.get(i));
		}
		dataset.addSeries(series);

		series = new XYSeries(bundle.getString("totalTitle"));
		for (int i = 0; i < reportTo.getLength(); i++) {
			series.add(i, totalList.get(i));
		}
		dataset.addSeries(series);

		return dataset;
	}

	public static XYMultipleSeriesDataset getFourOfNineBarDataset(
			DRMApplication drmApplication, Bundle bundle) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		NineOfFourReportTO reportTo = drmApplication.getDataHelper()
				.getAverageIntervals(bundle.getLong("userId"),
						bundle.getLong("actionId"),
						bundle.getString("userName"),
						bundle.getString("actionName"));
		List<Double> averageList = reportTo.getAverages();
		List<Long> totalList = reportTo.getTotals();

		CategorySeries series = new CategorySeries(bundle
				.getString("averageTitle"));
		for (int i = 0; i < reportTo.getLength(); i++) {
			series.add(averageList.get(i) / 100);
		}
		dataset.addSeries(series.toXYSeries());

		series = new CategorySeries(bundle.getString("totalTitle"));
		for (int i = 0; i < reportTo.getLength(); i++) {
			series.add(totalList.get(i) / 100);
		}
		dataset.addSeries(series.toXYSeries());

		return dataset;
	}	

	public static XYMultipleSeriesRenderer getBarRenderer() {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		SimpleSeriesRenderer r = new SimpleSeriesRenderer();
		r.setColor(Color.BLUE);
		renderer.addSeriesRenderer(r);
		r = new SimpleSeriesRenderer();
		r.setColor(Color.MAGENTA);
		renderer.addSeriesRenderer(r);
		renderer.setAxesColor(Color.BLACK);
		renderer.setLabelsColor(Color.BLACK);
		setChartSettings(renderer);

		return renderer;
	}
	
	public static XYMultipleSeriesDataset getFourOfNineHitBarDataset(
			DRMApplication drmApplication, Bundle bundle) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		NineOfFourReportTO reportTo = drmApplication.getDataHelper()
				.getHitCounts(bundle.getLong("userId"),
						bundle.getLong("actionId"),
						bundle.getString("userName"),
						bundle.getString("actionName"));
		List<Integer> correctHitList = reportTo.getCorrectHits();

		CategorySeries series = new CategorySeries(bundle.getString("correctHitLegend"));
		for (int i = 0; i < reportTo.getLength(); i++) {
			series.add(correctHitList.get(i));
		}
		dataset.addSeries(series.toXYSeries());

		return dataset;
	}	
	
	public static XYMultipleSeriesRenderer getHitBarRenderer() {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		SimpleSeriesRenderer r = new SimpleSeriesRenderer();
		r.setColor(Color.BLUE);
		renderer.addSeriesRenderer(r);
		renderer.setAxesColor(Color.BLACK);
		renderer.setLabelsColor(Color.BLACK);
		setHitChartSettings(renderer);

		return renderer;
	}

	private static void setChartSettings(XYMultipleSeriesRenderer renderer) {
		renderer.setChartTitle("Four Of Nine");
		renderer.setXTitle("x values");
		renderer.setYTitle("response time in ms");
		renderer.setXAxisMin(0.5);
		renderer.setXAxisMax(10.5);
		renderer.setYAxisMin(0);
		renderer.setYAxisMax(190);
	}
	
	private static void setHitChartSettings(XYMultipleSeriesRenderer renderer) {
		renderer.setChartTitle("Four Of Nine");
		renderer.setXTitle("Test");
		renderer.setYTitle("Number of Correct Hits");
		renderer.setXAxisMin(0.5);
		renderer.setXAxisMax(20.5);
		renderer.setYAxisMin(0);
		renderer.setYAxisMax(5);
	}
	
}
